import parole
from parole.colornames import colors
from parole.display import interpolateRGB
import pygame, random

import sim, sim_items, sim_creatures, main, interface
from util import *

class ScrollFaerie(sim_items.Scroll):
    def __init__(self):
        sim_items.Scroll.__init__(self, "faerie fire", "?", 
                ('light', 'life', 'far'),
                idDescription="Written in elegant sylvan cursive, this scroll "\
                    "causes a nearby corporeal foe to glow for a time with a "\
                    "mysterious light that clings to it even in death.") 
        
        self.lightSource = None 
        self.creature = None

    def read(self, reader):
        if reader is not main.player:
            # unsupported yet
            return

        def cancelCallback():
            main.transient('The scroll is wasted.')

        def callback(tile):
            if tile is main.player.parentTile:
                main.transient('Nothing seems to happen.')
                return

            for obj in tile:
                if isinstance(obj, sim_creatures.Creature):
                    #if not obj.leavesCorpse:
                    #    main.transient("%s seems unaffected." %
                    #            obj.noun(definite=1, capitalized=1), tile)
                    self.creature = obj
                    rgb = random.choice(['White', 'PaleTurquoise', 'Cyan',
                        'Pink', 'SpringGreen', 'Teal'])
                    self.lightSource = parole.map.LightSource(colors[rgb], 2.0)
                    self.lightSource.apply(self.map, obj.pos)
                    self.map.update()
                    reader.identify(self)
                    if main.mapFrame.fovObj and not main.player.dead:
                        main.mapFrame.touchFOV()
                    main.schedule.add(sim.Event('expire effect',
                        main.schedule.time + 4*60000, (self,), dests=[self]))
                    main.schedule.listen('enter tile', self)
                    main.schedule.listen('leave tile', self)
                    main.transient('%s begins to glow eerily.' %
                            obj.noun(definite=1, capitalized=1), obj.parentTile)
                    break

            if not self.lightSource:
                main.transient('Nothing seems to happen.')

        self.map = reader.parentTile.map
        interface.TargetTileInterface(callback, 
                cancelCallback=cancelCallback).push()

    def listen(self, event):
        sim_items.Scroll.listen(self, event)

        if event.id == 'expire effect' and event.args[0] is self:
            # TODO: what if the creature is dead by now?
            if visible(self.lightSource.pos):
                if self.creature.dead:
                    if self.creature.leavesCorpse:
                        main.transient("%s corpse stops glowing." %
                            self.creature.noun(definite=1, capitalized=1),
                            self.lightSource.pos)
                    else:
                        main.transient("The eerie glow fades away.",
                            self.lightSource.pos)
                else:
                    main.transient("%s stops glowing." %
                        self.creature.noun(definite=1, capitalized=1),
                        self.lightSource.pos)
            self.lightSource.remove(self.map)
            self.map.update()
            if main.mapFrame.fovObj and not main.player.dead:
                main.mapFrame.touchFOV()
            main.schedule.unlisten('enter tile', self)
            main.schedule.unlisten('leave tile', self)

        elif event.id == 'leave tile':
            obj, pos = event.args
            if obj is self.creature:
                self.lightSource.remove(self.map)
                if main.mapFrame.fovObj and not main.player.dead:
                    main.mapFrame.touchFOV()

        elif event.id == 'enter tile':
            obj, epos, emap = event.args
            if obj is self.creature:
                self.lightSource.apply(self.map,
                        self.creature.pos)
                self.creature.parentTile.map.update()

                if main.mapFrame.fovObj and not main.player.dead:
                    main.mapFrame.touchFOV()


#========================================
thingClass = ScrollFaerie


